<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class m_login extends CI_Model {

    public function getLoginData($usr, $pwd) {
        $u = mysql_real_escape_string($usr);
        $p = md5(mysql_real_escape_string($pwd));
        $q_cek_login = $this->db->get_where('tb_user', array('username' => $u, 'password' => $p));
        if (count($q_cek_login->result()) > 0) {
            foreach ($q_cek_login->result() as $qck) {
                foreach ($q_cek_login->result() as $qcd) {
                    $sess_data['logged_in'] = 'yesGetMeLogin';
                    $sess_data['username'] = $qcd->username;
                    //$sess_data['nama_pengguna'] = $qcd->nama_pengguna;
                    $this->session->set_userdata($sess_data);
                }
                header('location:' . base_url() . 'index.php/dashboard');
            }
        } else {
            $this->session->set_flashdata('result_login', 'Username atau Password yang anda masukkan salah.');
            header('location:' . base_url() . 'index.php/login');
        }
    }

        function login_client($usr, $pass) {
        $u = mysql_real_escape_string($usr);
        $p = md5(mysql_real_escape_string($pass));
        $cek_login = $this->db->get_where('tb_user', array('username' => $u, 'password' => $p));
        if (count($cek_login->result()) > 0) {
            foreach ($cek_login->result_array() as $data) {
                $hasil[] = $data;
            }
            return $hasil;
        } else {
            return false;
        }
    }

    function ambil() {
        $ambil = $this->db->get('tb_user');
        if ($ambil->num_rows() > 0) {
            foreach ($ambil->result() as $data) {
                $hasil[] = $data;
            }
            return $hasil;
        }
    }
    function hapus($a) {
        $this->db->trans_start();
        $this->db->delete('tb_user', array('id_user' => $a));
        $this->db->trans_complete();
        return;
    }
        function tambah(){
        $uname = $this->input->post('username');
        $pass = $this->input->post('password');
        $data1 = array('username' => $uname,'password' => md5($pass));
        $this->db->trans_start();
        $this->db->insert('tb_user', $data1);
        $this->db->trans_complete();
    }

}

?>